package com.serviceImpl;

import com.query.PersonQuery;
import com.entity.GoPerson;
import com.service.IPersonService;
import com.qzw.common.data.BaseMysqlDao;
import com.qzw.common.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
import java.lang.String;
import java.lang.Integer;
import com.model.Person;

@Service
public class PersonServiceImpl extends BaseServiceImpl<Person, GoPerson, Integer, PersonQuery> implements IPersonService {

	@Autowired
	private PersonDao dao;

	@Override protected BaseMysqlDao getDao() {
	return dao;
	}

	@Override
	public Person entity2model(GoPerson entity) {
		Person model = null;
		if(isNotEmpty(entity)){
			model = new Person();
			model.setId(entity.getId());
			model.setSex(entity.getSex());
		}
		return model;
	}

	@Override
	public GoPerson model2Entity(Person model) {
		GoPerson entity = null;
		if(isNotEmpty(model)){
			entity = new GoPerson();
			entity.setId(model.getId());
			entity.setSex(model.getSex());
		}
		return entity;
	}

	@Override
	public Person update(Person model) {
		if(model.isNew()){
			model = super.add(model);
		} else {
			Optional<GoPerson> entityOptional = dao.findById(model.getId());GoPerson entity = entityOptional.get();
			if(isNotEmpty(model.getSex()))
				entity.setSex(model.getSex());
			model = entity2model(dao.save(entity));
		}
		return model;
	}


}